package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.DamageListDao;
import com.atguigu.jxc.dao.DamageListGoodsDao;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.DamageListGoodsService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fenge
 */
@Service
public class DamageListGoodsServiceImpl implements DamageListGoodsService {

    @Autowired
    private DamageListDao damageListDao;

    @Autowired
    private DamageListGoodsDao damageListGoodsDao;

    @Override
    public void save(DamageList damageList, String damageListGoodsStr, HttpSession session) {
//        damageList.setDamageDate(DateUtil.DateToString(new Date(),"yyyy-MM-dd"));
        User user = (User) session.getAttribute("currentUser");
        Integer userId = user.getUserId();
//        String trueName = user.getTrueName();
        damageList.setUserId(userId);
        damageListDao.save(damageList);
        Gson gson = new Gson();
//        List<DamageListGoods> damageListGoods =  gson.fromJson(damageListGoodsStr, (Type) DamageListGoods.class);
//        List<DamageListGoods> damageListGoods = gson.fromJson(damageListGoodsStr, List.class);
        List<DamageListGoods> damageListGoods = gson.fromJson(damageListGoodsStr,new TypeToken<List<DamageListGoods>>(){}.getType());
        damageListGoods.forEach(damageListGood->{
            damageListGood.setDamageListId(damageList.getDamageListId());
            System.out.println(damageList.getDamageListId());
            damageListGoodsDao.save(damageListGood);
        });
//        damageListDao.save();
    }

    @Override
    public Map<String, Object> list(String sTime, String eTime) {
        List<DamageList> damageLists = damageListDao.list(sTime,eTime);
        Map<String, Object> map = new HashMap<>();
        map.put("rows",damageLists);
        return map;
    }

    @Override
    public Map<String, Object> goodsList(Integer damageListId) {
        List<DamageListGoods> damageListGoods = damageListGoodsDao.goodsList(damageListId);
        Map<String, Object> map = new HashMap<>();
        map.put("rows",damageListGoods);
        return map;
    }
}
/*[
{"goodsId":44,
"goodsCode":"0031",
"goodsName":"234",
"goodsModel":"234",
"goodsUnit":"元",
"goodsNum":"100",
"price":"100",

"total":10000}
"goodsTypeId":11,
"lastPurchasingPrice":0,

]
 */
